﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2021/5/14 17:13:38 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;
using System.IO;




namespace MTQ.Web.Controllers
{

    public class Log_ErrorController : BaseController
    {

        private ILog_ErrorService ilog_errorService = new Log_ErrorService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Log_Error_Index")]
        public ActionResult Index()
        {
            return View();
        }


        [AccountAuth(Roles = "Log_Error_Index")]
        public ActionResult Search()
        {


            ServertLayuiTable<Log_Error> servertBootStrapTable = new ServertLayuiTable<Log_Error>();
            GetSearchData(ref servertBootStrapTable, true);


            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }

        private void GetSearchData(ref ServertLayuiTable<Log_Error> servertBootStrapTable, bool page)
        {
            Log_ErrorQuery ilog_errorQuery = new Log_ErrorQuery();
            ilog_errorQuery.Searchkey = GetRequestQueryString("keyword");

            ilog_errorQuery.UserControlProjectNumber = UserControlProjectNumber;
            if (page)
            {
                servertBootStrapTable = ilog_errorService.LoadLayuiSearchData(ilog_errorQuery, Request);
            }
            else
            {
                IQueryable<Log_Error> ilog_error = ilog_errorService.GetIQueryable(ilog_errorQuery);
                servertBootStrapTable.data = ilog_error.ToList();
                servertBootStrapTable.count = ilog_error.Count();
            }
        }








        //          
        // GET: /Log_Error/Create          
        [AccountAuth(Roles = "Log_Error_Create")]
        public ActionResult Create()
        {
            Log_Error log_error = new Log_Error();
            return View(log_error);
        }

        //          
        // POST: /Log_Error/Create          
        [HttpPost]
        [AccountAuth(Roles = "Log_Error_Create")]
        public ActionResult Create(Log_Error log_error)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                var result = ilog_errorService.AddEntity(log_error);
                listMsg = new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败");
            }
            return Json(listMsg);
        }


        [AccountAuth(Roles = "Log_Error_Edit")]
        public ActionResult Edit(int? primarykey)
        {
            Log_Error log_error = ilog_errorService.LoadEntities(x => x.LE_Id == primarykey).FirstOrDefault();
            if (log_error == null)
            {
                return HttpNotFound();
            }
            return View(log_error);
        }

        [HttpPost]
        [AccountAuth(Roles = "Log_Error_Edit")]
        public ActionResult Edit(Log_Error formlog_error)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                try
                {
                    Log_Error log_error = ilog_errorService.LoadEntities(x => x.LE_Id == formlog_error.LE_Id).First();
                    log_error.LE_Id = formlog_error.LE_Id;
                    log_error.Log_UserName = formlog_error.Log_UserName;
                    log_error.Log_Path = formlog_error.Log_Path;
                    log_error.Log_HttpMethod = formlog_error.Log_HttpMethod;
                    log_error.Log_QueryString = formlog_error.Log_QueryString;
                    log_error.Log_ErrorInfo = formlog_error.Log_ErrorInfo;
                    log_error.Log_ErrorDetail = formlog_error.Log_ErrorDetail;
                    log_error.Log_CreateTime = formlog_error.Log_CreateTime;
                    bool result = ilog_errorService.UpdateEntity(log_error);
                    listMsg = new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败");
                }
                catch (Exception e)
                {
                    listMsg = new ModelMessage("ChangeResult", "m002", "保存失败");
                }
            }
            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Log_Error_Edit")]
        public ActionResult ActivateSwich(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }

        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<int> listLog_ErrorGUID = primarykeys.Split(',').ToListInt();
            List<Log_Error> listLog_Error = ilog_errorService.LoadEntities(x => listLog_ErrorGUID.Contains(x.LE_Id)).ToList();
            bool updaterow = ilog_errorService.DeleteListEntity(listLog_Error);
            if (updaterow)
            {
                return false;
            }
            return true;
        }


        public ActionResult Export()
        {

            var fieldtitle = GetRequestQueryString("fieldtitle");
            var listFieldTitle = JsonSerializerHelper.GetJsonList<FieldTitle>(fieldtitle);

            ServertLayuiTable<Log_Error> servertBootStrapTable = new ServertLayuiTable<Log_Error>();
            GetSearchData(ref servertBootStrapTable, false);
            List<ExcelColumn> columnInfo = new List<ExcelColumn>();

            if (listFieldTitle != null)
            {
                foreach (var item in listFieldTitle)
                {
                    columnInfo.Add(new ExcelColumn(item.field, item.title));
                }
            }

            byte[] btyBytes = null;
            var filename = Server.MapPath("/") + @"DownloadDocument\" + System.Guid.NewGuid().ToString() + ".xlsx";
            NPIOExportExcel.ExportExcelTest<Log_Error>(servertBootStrapTable.data, filename, ref btyBytes, columnInfo, servertBootStrapTable.data.Count);
            return File(filename, "text/comma-separated-values", "导出" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

        }





    }
}


